< previous page page_163 next page >

Page 163
Looping in VB
The ability to iterate over a series of statements is vital in any programming language. For example, you need to loop to retrieve items from a list box, and you need to loop to load items from a database table into a list box. Many other types of looping can occur in your programs. Two types of loop structures are available in Visual Basic: the Do...Loop and the For...Next loop. The Do...Loop construct offers many variations and is typically used if you dont know how many iterations will be performed. The For...Next loop is typically used when you know the exact number of iterations to perform on a block of code, see Figure 8.2.
8160-0163a.gif
Figure 8.2
The 
Do While loop example.
Examples Filename: DoWhile.frm
Do While
The first form of the Do...Loop youll learn about is the Do While construct. Do While tests a loop condition each time through the loop, and it keeps executing while the test expression is a True value. A Do...Loop is always terminated with a Loop statement. If the condition at the top of the loop is a True value, the statements between the Do and the Loop are executed. Its possible, with this form of loop, that the statements inside the loop may not even be executed. If the condition at the top of the loop evaluates to False, none of the statements inside of the block are executed. Heres the syntax:
9f31265bb40b592b1929e18a47ab28e5.gif
Do While <Cond>
    <Statement Block>
Loop

 
< previous page page_163 next page >

If you like this book, buy it!